package cn.zmwh.bbs.modules.bbs.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.zmwh.bbs.modules.bbs.mapper.ForumArticleMapper;
import cn.zmwh.bbs.modules.bbs.pojo.entity.ForumArticle;
import cn.zmwh.bbs.modules.bbs.pojo.entity.ForumArticleAttachment;
import cn.zmwh.bbs.modules.bbs.pojo.entity.ForumComment;
import cn.zmwh.bbs.modules.bbs.pojo.request.AttachmentRequest;
import cn.zmwh.bbs.modules.bbs.pojo.request.PublishRequest;
import cn.zmwh.bbs.modules.bbs.pojo.vo.ForumArticleVO;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.utils.IpInfo;
import cn.zmwh.common.core.utils.IpLocationUtil;
import cn.zmwh.common.core.utils.StringTools;
import cn.zmwh.common.security.util.HttpUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章信息 服务实现类
 * </p>
 *
 * @author dmzmwh
 * @since 2025-07-18
 */
@Service
public class ForumArticleService extends ServiceImpl<ForumArticleMapper, ForumArticle> {

    @Autowired
    private ForumArticleAttachmentService forumArticleAttachmentService;
    @Autowired
    private ForumCommentService forumCommentService;

    /**
     * 发布文章
     *
     * @param request
     * @param httpServletRequest
     * @return
     */
    public Result publish(PublishRequest request, HttpServletRequest httpServletRequest) {
        String ipAddr = HttpUtil.getIpAddr(httpServletRequest);
        IpInfo ipInfo = IpLocationUtil.ipResolve(ipAddr);
        String title = request.getTitle();
        title = StringTools.escapeTitle(title);
        ForumArticle forumArticle = new ForumArticle();
        Long id = request.getId();
        ForumArticle forumArticleById = baseMapper.selectById(id);
        if (id != null) {
            forumArticle.setId(id);
            forumArticle.setArticleNo(forumArticleById.getArticleNo());
            forumArticleAttachmentService.remove(Wrappers.<ForumArticleAttachment>lambdaQuery()
                    .eq(ForumArticleAttachment::getArticleNo, forumArticleById.getArticleNo())
                    .eq(ForumArticleAttachment::getUid, forumArticleById.getUid()));
        } else {
            String articleNo = request.getArticleNo();
            if (StrUtil.isNotBlank(articleNo)) {
                forumArticle.setTranslate(1);
                forumArticle.setArticleNo(articleNo);
            } else {
                forumArticle.setArticleNo(IdUtil.getSnowflakeNextIdStr());
            }
            forumArticle.setPBoardId(request.getPBoardId());
            forumArticle.setBoardId(request.getBoardId());
            forumArticle.setBoardName(request.getBoardName());
            forumArticle.setPBoardName(request.getPBoardName());
            forumArticle.setUid(request.getUid());
            forumArticle.setPostTime(new Date());
        }
        forumArticle.setTitle(title);
        forumArticle.setContent(request.getContent());
        forumArticle.setLang(request.getLang());
        forumArticle.setMarkdownContent(request.getMarkdownContent());
        forumArticle.setEditorType(request.getEditorType());
        forumArticle.setSummary(request.getSummary());
        forumArticle.setNickName(request.getNike());
        forumArticle.setUserIp(ipAddr);
        forumArticle.setUserIpAddress(ipInfo == null ? "" : ipInfo.getProvince() + "-" + ipInfo.getCity());
        forumArticle.setLastUpdateTime(new Date());
        forumArticle.setCover(request.getCover());
        Integer attachmentType = request.getAttachmentType();
        forumArticle.setAttachmentType(attachmentType);
        forumArticle.setStatus(0);
        //附件信息
        if (attachmentType == 1) {
            List<AttachmentRequest> attachments = request.getAttachments();
            List<ForumArticleAttachment> list = new ArrayList<>();
            for (AttachmentRequest attachment : attachments) {
                ForumArticleAttachment forumArticleAttachment = new ForumArticleAttachment();
                forumArticleAttachment.setIntegral(0);
                forumArticleAttachment.setArticleNo(forumArticle.getArticleNo());
                forumArticleAttachment.setUid(request.getUid());
                forumArticleAttachment.setFileSize(attachment.getFileSize());
                forumArticleAttachment.setFileName(attachment.getFileName());
                forumArticleAttachment.setFilePath(attachment.getFilePath());
                forumArticleAttachment.setFileType(attachment.getFileType());
                list.add(forumArticleAttachment);
            }
            forumArticleAttachmentService.saveBatch(list);
        }
        baseMapper.insert(forumArticle);
        return Result.ok(forumArticle.getId());
    }

    public Result<ForumArticleVO> detail(Long uid, String id) {

        ForumArticle forumArticle = this.getById(id);
        ForumArticleVO detailVO = new ForumArticleVO();
        BeanUtil.copyProperties(forumArticle, detailVO);
        if (forumArticle.getAttachmentType() == 1) {
            String articleNo = forumArticle.getArticleNo();
            List<ForumArticleAttachment> list = forumArticleAttachmentService.list(Wrappers.<ForumArticleAttachment>lambdaQuery()
                    .eq(ForumArticleAttachment::getArticleNo, articleNo)
                    .eq(forumArticle.getTranslate() != 1, ForumArticleAttachment::getUid, forumArticle.getUid()));
            if (CollUtil.isNotEmpty(list)) {
                detailVO.setAttachments(list);
            }
        }


        return Result.ok(detailVO);
    }
}
